package com.galaxy.product.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.galaxy.common.core.domain.R;
import com.galaxy.common.core.validate.AddGroup;
import com.galaxy.common.core.validate.EditGroup;
import com.galaxy.common.log.annotation.Log;
import com.galaxy.common.log.enums.BusinessType;
import com.galaxy.common.mybatis.core.page.PageQuery;
import com.galaxy.common.mybatis.core.page.TableDataInfo;
import com.galaxy.product.domain.ProductCategory;
import com.galaxy.product.domain.bo.ProductCategoryBo;
import com.galaxy.product.domain.vo.ProductCategoryVo;
import com.galaxy.product.service.IProductCategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类控制器
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/category")
public class ProductCategoryController {

    private final IProductCategoryService categoryService;

    /**
     * 查询商品分类列表
     */
    @GetMapping("/list")
    @SaCheckPermission("product:category:list")
    public TableDataInfo<ProductCategoryVo> list(ProductCategoryBo bo, PageQuery pageQuery) {
        return categoryService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询商品分类树结构
     */
    @GetMapping("/tree")
    @SaCheckPermission("product:category:list")
    public R<List<ProductCategoryVo>> tree() {
        return R.ok(categoryService.queryTree());
    }

    /**
     * 获取商品分类详细信息
     */
    @GetMapping("/{id}")
    @SaCheckPermission("product:category:list")
    public R<ProductCategory> getInfo(@PathVariable Long id) {
        return R.ok(categoryService.getInfo(id));
    }

    /**
     * 新增商品分类
     */
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping
    @SaCheckPermission("product:category:add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProductCategoryBo bo) {
        categoryService.add(bo);
        return R.ok();
    }

    /**
     * 修改商品分类
     */
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    @SaCheckPermission("product:category:update")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProductCategoryBo bo) {
        categoryService.update(bo);
        return R.ok();
    }

    /**
     * 删除商品分类
     */
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @SaCheckPermission("product:category:delete")
    public R<Void> remove(@PathVariable Long[] ids) {
        categoryService.deleteBatch(ids);
        return R.ok();
    }
}
